package com.og.ouroa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.Course;
import com.og.ouroa.mapper.CourseMapper;
import com.og.ouroa.pojo.entity.StudentNode;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.CourseVO;
import com.og.ouroa.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public IPage<Course> list(Page<Course> page) {

        return baseMapper.selectPage(page, null);


    }

    @Override
    public IPage<Course> listBySid(Page<Course> page, Integer sid) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",sid);
        return baseMapper.selectPage(page,wrapper);



    }

    @Override
    public Course getByCid( Integer cid) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("id",cid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Course getByC_number(String c_number) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("c_number", c_number);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void imputUrlbyId(Integer cid, String url) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",cid);



        Course course = baseMapper.selectOne(queryWrapper);
        Assert.notNull(course, ResponseEnum.ABNORMAL_ACCOUNT);
        course.setImg(url);
        baseMapper.updateById(course);

    }

    @Override
    public void addCourse(CourseVO courseVO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVO,course);
        baseMapper.insert(course);



    }

    @Override
    public void updataStatus(Integer id,Integer status) {
        Course course = baseMapper.selectById(id);
        course.setStatus(status);
        baseMapper.updateById(course);



    }
}
